﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CodeGame.ServerObjects;

namespace CodeGame.Networking.Serverside.ClientServerPackets
{
    class DatabasePacket : ServerPacket
    {
        public override ClientServerNet GetID()
        {
            return ClientServerNet.DatabaseRequest;
        }

        public override void Handle(Lidgren.Network.NetIncomingMessage message, Lidgren.Network.NetConnection sender)
        {
            Player sendingPlayer = ServerSingletons.Players.GetPlayer(sender);

            int command = message.ReadInt32();
            string table = message.ReadString();
            string key = message.ReadString();
            string value = message.ReadString();

            switch ((DatabaseCommands)(Convert.ToInt32(command)))
            {
                case DatabaseCommands.GetKey:
                    ServerProtocolChat.SendPrivateChat("DATABASE", ServerSingletons.Database.GetDatabase(sendingPlayer).GetKey(table, key), sender);
                    break;
                case DatabaseCommands.SetKey:
                    ServerSingletons.Database.GetDatabase(sendingPlayer).SetKey(table, key, value);
                    ServerProtocolChat.SendPrivateChat("DATABASE", "Ok", sender);
                    break;
                case DatabaseCommands.AddValue:
                    ServerSingletons.Database.GetDatabase(sendingPlayer).AddValue(table, value);
                    break;
                case DatabaseCommands.GetAllKeys:
                    ServerSingletons.Database.GetDatabase(sendingPlayer).GetAllKeys(table);
                    break;
                default:
                    ServerProtocolChat.SendPrivateChat("DATABASE", "Command not Found", sender);
                    break;
            }
        }
    }
}
